<?php
/**
 * Created by PhpStorm.
 * User: haohao
 * Date: 2021/12/31
 * Time: 16:32
 */

namespace App\Admin\Metrics;

use App\Models\OrderModel;
use App\Models\Recharge;
use  Dcat\Admin\Admin;
use Dcat\Admin\Widgets\ApexCharts\Chart;
use Illuminate\Support\Carbon;

class UserBar extends Chart {
    public function __construct($containerSelector = null, $options = [])
    {
        parent::__construct($containerSelector, $options);

        $this->setUpOptions();
    }

    /**
     * 初始化图表配置
     */
    protected function setUpOptions()
    {
        $color = Admin::color();
        $this->title('七日销售额');
        $colors = [$color->primary(), $color->primaryDarker()];

        $this->options([
            'colors' => $colors,
            'chart' => [
                'type' => 'bar',
                'height' => 430
            ],
            'plotOptions' => [
                'bar' => [
                    'horizontal' => false,
                    'dataLabels' => [
                        'position' => 'top',
                    ],
                ]
            ],
            'dataLabels' => [
                'enabled' => true,
                'offsetX' => 0,
                'style' => [
                    'fontSize' => '12px',
                    'colors' => ['#fff']
                ],
            ],
            'stroke' => [
                'show' => true,
                'width' => 1,
                'colors' => ['#fff']
            ],
            'xaxis' => [
                'categories' => [],
            ],
        ]);
    }

    /**
     * 处理图表数据
     */
    protected function buildData()
    {
//        // 执行你的数据查询逻辑
//        $user7 = OrderModel::query()
//            ->selectRaw('date(created_at) as date, sum(cost_price) as total')
//            ->whereBetween('created_at', [Carbon::today()->subDays(7), Carbon::today()])
//            ->groupBy('date')
//            ->get()
//            ->toArray();
        $date7 = [];
        $price = [];
        for ($i = 7; $i--;) {
            $date = Carbon::yesterday()->subDays($i)->format('Y-m-d');
            $date7[] = $date;
            $price[] =  OrderModel::query()->whereBetween('created_at', [$date.' 00:00:00', $date.' 23:59:59'])->sum('pay_amount');;
        }
        $data = [
            [
                'data' => [$price[0], $price[1], $price[2], $price[3], $price[4], $price[5], $price[6]]
            ]
        ];
        $categories = [$date7[0], $date7[1], $date7[2], $date7[3], $date7[4], $date7[5],$date7[6]];

        $this->withData($data);
        $this->withCategories($categories);
    }

    /**
     * 设置图表数据
     *
     * @param array $data
     *
     * @return $this
     */
    public function withData(array $data)
    {
        return $this->option('series', $data);
    }

    /**
     * 设置图表类别.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withCategories(array $data)
    {
        return $this->option('xaxis.categories', $data);
    }

    /**
     * 渲染图表
     *
     * @return string
     */
    public function render()
    {
        $this->buildData();

        return parent::render();
    }
}
